using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SyncUdp
{
    using Interface;
    using System;

    /// <summary>
    /// 抽象 Mono 的单例
    /// </summary>
    public abstract class AbstractMonoSingleton : MonoBehaviour, ISingleton
    {
        protected IGameManager _gm;

        protected IUIManager _uiManager;

        protected IEventCenter _eventCenter;

        #region 接口实现
        public ISingleton Instance => this;

        public bool IsInited { get; private set; }

        void Start()
        {
            Init();
        }

        public void Init()
        {
            if (IsInited)
                return;
            IOCContainer.AddInstance(this.GetType(), this);
            _gm = IOCContainer.GetInstance<IGameManager>();
            _uiManager = IOCContainer.GetInstance<IUIManager>();
            _eventCenter = IOCContainer.GetInstance<IEventCenter>();
            InitOnStart();
            IsInited = true;
        }
        #endregion

        /// <summary>
        /// 在 Start 方法中进行初始化
        /// </summary>
        protected abstract void InitOnStart();

        protected void RegisterEvent<T>(Action<T> action) where T : IEventArgs
        {
            _eventCenter.RegisterEvent<T>(action);
        }

        protected void UnregisterEvent<T>() where T : IEventArgs
        {
            _eventCenter.UnregisterEvent<T>();
        }

        protected void SendEvent<T>(T eventArgs) where T : IEventArgs
        {
            _eventCenter.SendEvent(eventArgs);
        }

    }
}
